var fd_leftmenu_contextHtml =  "<div class=\"jscontextBtn\" onclick ='managerhotel_addProduct(this)' ><img src='images/menu/addproduct.png' class='imgcontext'/>Thêm món ngoài menu</div>"
                                    +"<div class=\"jscontextBtn\" onclick ='managerhotel_moveRoom(this)' ><img src='images/menu/move_1.png' class='imgcontext'/>Chuyển phòng</div>" 
                                    +"<div class=\"jscontextBtn\" onclick ='htfrmmanagerhotel_customerInfo(this)' ><img src='images/menu/cusinfo.png' class='imgcontext'/>Thông tinh khách hàng</div>"  ;
 var fd_activeroom_contextHtml =  '';
 var ht_obj   = new classRoom();
$(document).ready(function () {
    
    $('#htfrmmanagerhotel_divAddProduct').dialog({
        autoOpen: false,
        modal: false,
        width: 400,
        resizable: true,
        overflow: 'auto' 
    });
    initForm('htfrmmanagerhotel_tblAddProduct');
    $('#htfrmmanagerhotel_moveRoom').dialog({
        autoOpen: false,
        modal: false,
        width: 300,
        resizable: true,
        overflow: 'auto' 
    });
    
    $('#htfrmmanagerhotel_billOrder').dialog({
        autoOpen: false,
        modal: false,
        width: 350,
        resizable: true,
        overflow: 'auto' 
    });
    
    $('#htfrmmanagerhotel_divCustomer').dialog({
        autoOpen: false,
        modal: false,
        width: 800,
        resizable: true,
        overflow: 'auto' 
    });
    
        
    $('.deleteMenu').live('click',function(){
      var  flg_one = true;
      var nameid = $(this).attr('id');
      prodId = getId(nameid,1);
      current_id = getId(nameid,2);
      ht_obj.removeProduct(prodId,current_id);
        $(this).parent().find('.menuitem').each(function(){
                var fulltext = $(this).text();
                start = fulltext.indexOf("(");
                end = fulltext.indexOf(")"); 
                num = $.trim(fulltext.substring(start+1,end));
                num = parseInt(num);
                if(num>1){
                    flg_one = false;
                     num--;
                     var itemname = $.trim(fulltext.substring(0,start));   
                     $(this).text(itemname+'('+num+')');
                }
        });
        if(flg_one){
           $(this).parent().remove(); 
        }
        
    });
    
    $('.down').live('click',function(){

      var nameid = $(this).attr('id');
      prodId = getId(nameid,1);
      current_id = getId(nameid,2);
      var btnid = prodId+'_'+current_id;     
      ChangeTr("htfrmmanagerhoteltr_"+btnid,1);
      ht_obj.removeProduct(prodId,current_id);
      var trParent = $(this).parents('tr:eq(0)');
      var numProduct = $(trParent).find('.quanlity:first').text();
      numProduct = parseInt(numProduct);
      if(numProduct==1){
        $(trParent).remove();  
      }else{
          numProduct --;
        $(trParent).find('.quanlity:first').text(numProduct);  
      }
        
    });
    
    $('.up').live('click',function(){

      var nameid = $(this).attr('id');
      prodId = getId(nameid,1);
      current_id = getId(nameid,2);
        var btnid = prodId+'_'+current_id;     
      ChangeTr("htfrmmanagerhoteltr_"+btnid,0);
      ht_obj.addProduct(prodId,current_id);
      var trParent = $(this).parents('tr:eq(0)');
      var numProduct = $(trParent).find('.quanlity:first').text();
      numProduct = parseInt(numProduct);

      numProduct++;
      $(trParent).find('.quanlity:first').text(numProduct);  
  
        
    });
    
     $('.clsbtn').live('click', function(){
          var id = $(this).attr('id');
          var arrname = new Array();
          arrname = id.split('_');
          room_id = arrname[2];
          if(ht_obj.arrRoom[room_id].status==0){
             xajax_htfrmmanagerhotel_checkedRoom(room_id); 
          }else{
              
            
              frmmanagerhotet_execute();
              roomtype = ht_obj.arrRoom[room_id].roomtype;
              generate_id = ht_obj.arrRoom[room_id].current_id;
              arrRoomPrice = ht_obj.arrRoomPrice[roomtype];
              startTime = ht_obj.startTime;
              flgCal = ht_obj.flgCal;
              endTime = ht_obj.endTime;
              $('#htfrmmanagerhotel_roomId').val(room_id);
              title = 'Hóa đơn tính tiền phòng ' +ht_obj.arrRoom[room_id].name;
              $("#htfrmmanagerhotel_billOrder").dialog('option', 'title', title);  
              xajax_htfrmmanagerhotel_ajxTotalRoom(room_id,arrRoomPrice,startTime,endTime,generate_id,flgCal);
          }
         
     });
     
     $('.clsFinish').live('click', function(){
          room_id=$('#htfrmmanagerhotel_roomId').val();
          roomPrice=$.trim($('#htfrmmanagerhotel_tdroomPrice').text());
          totalPrice=$.trim($('#htfrmmanagerhotel_tdAllTotal').text());
          roomPrice = roomPrice.replace(/,/gi,'');          
          totalPrice = totalPrice.replace(/,/gi,'');          
         // generate_id = ht_obj.arrRoom[room_id].current_id;
          ht_obj.createAssignStatus(room_id,0,0,'');
          ht_obj.arrCustomer[room_id]= new Object();
          xajax_htfrmmanagerhotel_finishRoom(room_id,roomPrice,totalPrice);
          $('#htfrmmanagerhotel_billOrder').dialog('close');
         
     });
     

});                    

function htfrmmanagerhotel_dropdrapmenu(){
        var myMenu;
        myMenu = new SDMenu("htfrmmanagerhotel_leftmenu");
        myMenu.init();
        $( "#htfrmmanagerhotel_leftmenu a" ).draggable({
            appendTo: "body",
            helper: "clone"
        });
        $(".clsOn").draggable({
            appendTo: "body",
            //helper: "clone",
            revert: true
        });        
        $( ".cf_clsOff").droppable({
            activeClass: "ui-state-default",
            hoverClass: "ui-state-hover",
            accept: '.clsOn',
            drop: function( event, ui ) {
                var  trIdFrm  = ui.draggable.parents('tr:eq(0)').attr('id') ;                
                var  trIdTo   = $(this).parents('tr:eq(0)').attr('id') ; 
                rfrm = getId(trIdFrm,2);               
                rto = getId(trIdTo,2); 
                htfrmmanagerhotel_moveRoom(rfrm,rto);              
            }
        });
        
        $( ".clsdrop  div.clsdrop" ).droppable({
            activeClass: "ui-state-default",
            hoverClass: "ui-state-hover",
           // accept: ":not(.ui-sortable-helper)",
            accept: '#htfrmmanagerhotel_leftmenu a',
            drop: function( event, ui ) {
                var name = ui.draggable.text() ;
               // name = $.trim(name.substring(0,name.indexOf("(")-1));
                var flg_exist = false;
                nameId =   ui.draggable.attr('id') ;
                
                proId =  getId(nameId,2) ;
                roomId = getId($(this).attr('id'),2);
                current_id = ht_obj.arrRoom[roomId].current_id ;
                ht_obj.addProduct(proId,current_id);
                $(this).find('.trProduct').each(function(){  
                trnameId =   $(this).attr('id') ;
                trproId =  getId(trnameId,1) ;
                if(proId==trproId){
                   flg_exist=true;
                      var numProduct = $(this).find('.quanlity:first').text();
                      numProduct = parseInt(numProduct);
                      numProduct++;
                      $(this).find('.quanlity:first').text(numProduct);
                      btnid = proId+'_'+current_id;
                      ChangeTr("htfrmmanagerhoteltr_"+btnid,0);
                        
                   
                   //break; 
                }
                                  
                }); 
                //alert(name);
                if(!flg_exist){// trường hợp đã tồn tại món này
                    btnid = proId+'_'+current_id;
                 // $( "<div><img height='10' title='bỏ 1 sản phẩm' id='htfrmmanagerhotel_"+btnid+"' class ='deleteMenu' src='images/icons/DeleteRed.png'/>&nbsp;<span class='menuitem'>"+name+"(1)</span></div>" ).appendTo( this ); 
                 
                  $( '<tr class="trProduct" id="htfrmmanagerhoteltr_'+btnid+'"><td width="70%" align="left" class="productName">'+name+'</td><td width="10%" align="right" class="quanlity">1</td><td width="10%" align="center"><img class="down"  src="adagps.com/images/product/down.png" title="Bot mot san pham"/ width="10" height="10" id="htfrmmanagerhoteldowm_'+btnid+'"></td><td width="10%" align="center"><img class="up"  src="adagps.com/images/product/up.png" title="Bot mot san pham"/ width="10" height="10" id="htfrmmanagerhtup_'+btnid+'"></td></tr>' ).appendTo( $(this).find('table:first'));  
                  ChangeTr("htfrmmanagerhoteltr_"+btnid,0);
                }
                
            }
        }).sortable({
            items: "li:not(.placeholder)",
            sort: function() {
                $( this ).removeClass( "ui-state-default" );
            }
        });
 
}


function assignTime(){
    setTimeout('assignTime()',1000);   
    var timeCurrent = Date.now();
    offset = Math.round((timeCurrent-timePrevious)/1000);
    offset = offset<1?1:offset;
    
    for(var i in ht_obj.arrRoom){
        obj = ht_obj.arrRoom[i];
        status = obj.status;
        timestamp = obj.timestamp;
        if(status==1){
           timestamp = parseInt(timestamp)+offset;
           ht_obj.arrRoom[i].timestamp = timestamp;
           strtime = creaeteTime(timestamp,'m');
           $('#htfrmmanagerhotel_td'+i).html(strtime);
           $("#htfrmmanagerhotel_btnrent_"+i ).text("Hóa Đơn");
           $('#htfrmmanagerhotel_div_'+i).css('display','');
           $('#htfrmmanagerhotel_tr_'+i+' div.clsOn').css('display','');
           $('#htfrmmanagerhotel_tr_'+i+' div.cf_clsOff').css('display','none');
           
        }else{
            $('#htfrmmanagerhotel_div_'+i).text('');
            $('#htfrmmanagerhotel_div_'+i).css('display','none');  
            $( "#htfrmmanagerhotel_btnrent_"+i ).text("Thuê");
            $('#htfrmmanagerhotel_td'+i).text('');
            $('#htfrmmanagerhotel_tr_'+i+' div.clsOn').css('display','none');
            $('#htfrmmanagerhotel_tr_'+i+' div.cf_clsOff').css('display','');
           ht_obj.arrRoom[i].timestamp = 0; 
        }
    }
    timePrevious=timeCurrent;
    fd_activeroom_contextHtml = htfrmmanagerhotel_createListRoom();
    // addJsContext(); 
    // alert(fd_activeroom_contextHtml);
}

// tao chuoi thoi gian tu so giay nhap vao
// time: so giay
//function creaeteTime(time){   
// h = parseInt(time/3600);
// m = parseInt((parseInt(time%3600))/60);
// s = parseInt(time%60);
// h = h<10?'0'+h:h;
// m = m<10?'0'+m:m;
// s = s<10?'0'+s:s; 
// return h+":"+m+":"+s;
//}

function executeAction(){
  setTimeout('executeAction()',10000);
  frmmanagerhotet_execute(); 
         
}

function frmmanagerhotet_execute(){
    var flgAddRemove = false;
  var flgRemove = false;
  var flgMove = false;
  var arrAddRemove = new Array();
  var arrMove = new Array();
  var index1 = 0;// chi số cho mãng thêm hoặc xóa sản phẩm
  var index2 = 0;// chi số cho việc chuyển phòng.
     for(var i in ht_obj.arrProductAdd){
          if(typeof(ht_obj.arrProductAdd[i])!='function')
          {
               nump = ht_obj.arrProductAdd[i];
              if(nump>0){
                  flgAddRemove = true;
                  arrAddRemove[index1] =  i+';'+ nump;
                  index1++;
              } 
           }
      }
   for(var i in ht_obj.arrProductRemove){
        if(typeof(ht_obj.arrProductRemove[i])!='function')
        {
            nump = ht_obj.arrProductRemove[i];
            if(nump>0){
                flgAddRemove = true;                    
                nump =-nump;
                arrAddRemove[index1] =  i+';'+ nump;
                index1++;                  
                
            } 
        } 
   }
   if(flgAddRemove){
       xajax_htfrmmanagerhotel_addRemoveProduct(arrAddRemove,ht_obj.arrProductPrice);            
   }

   for(var i in ht_obj.arrRoomMove){
       if(typeof(ht_obj.arrRoomMove[i])!='function')
        {
           flg = ht_obj.arrRoomMove[i];
          if(flg){
              flgMove = true;
              arrMove[index2]=  i;
              index2++;
             
          } 
       }
  }
  if(flgMove){
       xajax_htfrmmanagerhotel_moveRoom(arrMove); 
  }
   
   ht_obj.resetAction();
    
}

function getId(name,pos){
    var arrName = name.split('_');
    if(pos >= arrName.length||pos == 'undefined'){
        return arrName[arrName.length-1] ;
    }
    return arrName[pos] ;    
}

function frmLoadProduct(room_id,generate_id,product_id,product_name,num){
    var btnid = product_id+'_'+generate_id;
    $( '<tr class="trProduct" id="htfrmmanagerhoteltr_'+btnid+'"><td width="70%" align="left" class="productName">'+
    product_name+'</td><td width="10%" align="right" class="quanlity">'+num
    +'</td><td width="10%" align="center"><img class="down"  src="adagps.com/images/product/down.png" title="Bot mot san pham"/ width="10" height="10" id="htfrmmanagerhoteldowm_'   +btnid+'"></td><td width="10%" align="center"><img class="up"  src="adagps.com/images/product/up.png" title="Bot mot san pham"/ width="10" height="10" id="htfrmmanagerhtup_'+btnid+'"></td></tr>' ).appendTo(  $('#htfrmmanagerhotel_div_'+room_id).find('table:first') );  

}
function addJsContext(){
    $("div.divProduct").jscontext({
        html: fd_leftmenu_contextHtml ,               
        fade: false 
    }); 
    $("#htfrmmanagerhotel_leftmenu a").jscontext({
        html: fd_activeroom_contextHtml ,               
        fade: false 
    }); 
    

    
   // fd_activeroom_contextHtml =htfrmmanagerhotel_createListRoom(); 
    
}
function managerhotel_addProduct(obj){
  $('#htfrmmanagerhotel_divAddProduct').dialog('option', 'title', 'Thêm sản phẩm ngoài danh mục');  
  $('#htfrmmanagerhotel_divAddProduct').dialog('open');
      var divName = $(obj).parent().attr('id');
    arrName = divName.split('_');
    id = parseInt(arrName[2]);
    $("#htfrmmanagerhotel_hdroomAdd").val(id);
  $('.jscontextBtn').hide();
}

function managerhotel_moveRoom(obj){
   
    $("#htfrmmanagerhotel_roomactive").text('');
    var divName = $(obj).parent().attr('id');
    arrName = divName.split('_');
    id = parseInt(arrName[2]);
    roomMove = ht_obj.arrRoom[id].name;
    $("#htfrmmanagerhotel_roomMoveId").val(id);
    var title = 'Chuyển từ phòng '+roomMove; 
    flg_roomactive = false;
    for(var i in ht_obj.arrRoom){
       if(typeof(ht_obj.arrRoom[i])!='function')
        {
           status = ht_obj.arrRoom[i].status;
           name = ht_obj.arrRoom[i].name;
           if(status==0){
               $("#htfrmmanagerhotel_roomactive").append('<option value="'+i+'">P.'+name+'</option>');
               flg_roomactive = true;
           }          
       }
  }
  if(flg_roomactive){
     $('#htfrmmanagerhotel_moveRoom').dialog('option', 'title',title);  
     $('#htfrmmanagerhotel_moveRoom').dialog('open');   
  }else{
      alert('Không còn phòng trống để chuyển.');
  }

  $('.jscontextBtn').hide();
}

function htfrmmanagerhotel_moveRoom(rfrm,rto){
    var roomFrom = (typeof rfrm !='undefined'?rfrm:$('#htfrmmanagerhotel_roomMoveId').val());
    var roomTo = (typeof rto!='undefined'?rto:$('#htfrmmanagerhotel_roomactive').val());     
    
    var product = $('#htfrmmanagerhotel_div_'+roomFrom).html(); 
    objCustomer =  ht_obj.arrCustomer[roomFrom];
    ht_obj.arrCustomer[roomTo]=new Object();
    for(var i in objCustomer){
        name = objCustomer[i].name;
        cmnd = objCustomer[i].cmnd;
        age = objCustomer[i].age;
        address = objCustomer[i].address;
        sex = objCustomer[i].sex;
        moto = objCustomer[i].moto;
       objCustomer[i] =  ht_obj.addCustomer('','','','','','');       
       ht_obj.arrCustomer[roomTo][i]=ht_obj.addCustomer(name,cmnd,age,address,sex,moto);
    }
    
    current_id = ht_obj.arrRoom[roomFrom].current_id;
    timestamp  =ht_obj.arrRoom[roomFrom].timestamp;
    ht_obj.arrRoom[roomTo].status = 1;
    ht_obj.arrRoom[roomTo].timestamp = timestamp;
    ht_obj.arrRoom[roomTo].current_id =current_id;
    ht_obj.arrRoom[roomFrom].status = 0;
    ht_obj.arrRoom[roomFrom].timestamp =0;
    ht_obj.arrRoom[roomFrom].current_id ='';        
    $('#htfrmmanagerhotel_div_'+roomFrom).css('display','none');
    $('#htfrmmanagerhotel_div_'+roomFrom).text('');
    $('#htfrmmanagerhotel_div_'+roomTo).css('display','');
   $('#htfrmmanagerhotel_div_'+roomTo).append(product); 
    xajax_htfrmmanagerhotel_moveRoomNew(roomFrom,roomTo);
    $('#htfrmmanagerhotel_moveRoom').dialog('close');
}

function htfrmmanagerhotel_addNewProduct(){

    proId = ht_obj.indexNewProduct;
    hdroom = $('#htfrmmanagerhotel_hdroomAdd').val();
    current_id = ht_obj.arrRoom[hdroom].current_id;
    var name = $.trim($('#htfrmmanagerhotel_productName').val());   
    var price = $.trim($('#htfrmmanagerhotel_productPrice').val()); 
    xajax_htfrmmanagerhotel_addNewProduct(proId,current_id,name,price);     
    var btnid = proId+'_'+current_id;
       $( '<tr class="trProduct" id="htfrmmanagerhoteltr_'+btnid+'"><td width="70%" align="left" class="productName">'+name+'</td><td width="10%" align="right" class="quanlity">1</td><td width="10%" align="center"><img class="down"  src="adagps.com/images/product/down.png" title="Bot mot san pham"/ width="10" height="10" id="htfrmmanagerhoteldowm_'+btnid+'"></td><td width="10%" align="center">&nbsp;</td></tr>' ).appendTo(  $('#htfrmmanagerhotel_div_'+hdroom).find('table:first') );   
   ChangeTr("htfrmmanagerhoteltr_"+btnid,0);
    ht_obj.indexNewProduct++;
        $('#htfrmmanagerhotel_divAddProduct').dialog('close');
    
}
function htfrmmanagerhotel_createListRoom(proname,id){
    var strlist = "<div><div>"+proname+"</div><ul class='ulproduct'>";
     for(var i in ht_obj.arrRoom){
       if(typeof(ht_obj.arrRoom[i])!='function')
        {
           status = ht_obj.arrRoom[i].status;
           name = ht_obj.arrRoom[i].name;
           current_id = ht_obj.arrRoom[i].current_id;
           if(status==1){
               strlist += '<li onclick="addProductLi('+i+','+id+')"><img height="10px" width="20px" src="adagps.com/images/menu/forward.ico"/>P.'+name+'</li>';
               flg_roomactive = true;
           }          
       }
  }
  strlist +='</ul></div>';
return strlist;
    
}

function addProductLi(roomId,proId){    
    current_id = ht_obj.arrRoom[roomId].current_id ;
    name = ht_obj.arrProductPrice[proId].name;
    ht_obj.addProduct(proId,current_id);
    var  flg_exist = false;  
    $('#htfrmmanagerhotel_div_'+roomId).find('.trProduct').each(function(){     
    trnameId =   $(this).attr('id') ;
    trproId =  getId(trnameId,1) ;   
    if(proId==trproId){
        flg_exist=true;
        var numProduct = $(this).find('.quanlity:first').text();
        numProduct = parseInt(numProduct);
        numProduct++;
        $(this).find('.quanlity:first').text(numProduct);
        btnid = trproId+'_'+current_id;
        ChangeTr("htfrmmanagerhoteltr_"+btnid,0);

    }
                      
    }); 
    if(!flg_exist){
        btnid = proId+'_'+current_id;
        $( '<tr class="trProduct" id="htfrmmanagerhoteltr_'+btnid+'"><td width="70%" align="left" class="productName">'+name+'</td><td width="10%" align="right" class="quanlity">1</td><td width="10%" align="center"><img class="down"  src="adagps.com/images/product/down.png" title="Bot mot san pham"/ width="10" height="10" id="htfrmmanagerhoteldowm_'+btnid+'"></td><td width="10%" align="center"><img class="up"  src="adagps.com/images/product/up.png" title="Bot mot san pham"/ width="10" height="10" id="htfrmmanagerhtup_'+btnid+'"></td></tr>' ).appendTo( $('#htfrmmanagerhotel_div_'+roomId).find('table:first'));      
        ChangeTr("htfrmmanagerhoteltr_"+btnid,0);
    }
}
function htfrmmanagerhotel_customerInfo(obj){
    var divName = $(obj).parent().attr('id');
    arrName = divName.split('_');
    id = parseInt(arrName[2]);
    current_id = ht_obj.arrRoom[id].current_id;
    var objCustomer = ht_obj.arrCustomer[id];
    flg_exist = false;
    var strtblheader = '<tr><td width="26%">Tên</td><td width="12%">Số cmnd</td><td width="4%">Tuổi</td><td width="30%">Địa chỉ</td><td width="7%">Phái</td><td width="12%">Số xe</td><td width="9%">sửa/xóa</td></tr>';
    var strtbl ='';
    for(var i in objCustomer){
       if(typeof(objCustomer[i])!='function'&&(objCustomer[i].name!=''||objCustomer[i].moto!='')){
           
           name = objCustomer[i].name;
           cmnd = objCustomer[i].cmnd;
           moto = objCustomer[i].moto;
           address  = objCustomer[i].address;
           age  = objCustomer[i].age;
           sex  = (objCustomer[i].sex==0?'Nam':'Nữ');
           flg_exist = true;
           trIdCustomer = current_id+'_'+i;
           strtbl +='<tr id="htfrmmanagerhoteltr_'+trIdCustomer+'"><td id="htfrmmanagerhoteltdname_'+trIdCustomer+'">'+name+'</td><td id="htfrmmanagerhoteltdcmnd_'+trIdCustomer+'">'+cmnd+
           '</td><td id="htfrmmanagerhoteltdage_'+trIdCustomer+'">'+age+'</td><td  id="htfrmmanagerhoteltdaddress_'+trIdCustomer+'">'+address
           +'</td><td id="htfrmmanagerhoteltdsex_'+trIdCustomer+'">'+sex+'</td><td  id="htfrmmanagerhoteltdmoto_'+trIdCustomer+'">'+moto+'</td><td id="htfrmmanagerhoteltdedit_'+trIdCustomer+'"><img class="imgSmall" src="adagps.com/images/menu/edit.png"  onclick="htfrmmanagerhotel_editCustomer(this)" id="htfrmmanagerhotelimgedit_'+trIdCustomer
           +'">&nbsp;&nbsp;<img class="imgSmall" src="adagps.com/images/menu/red_delete.png" onclick="htfrmmanagerhotel_deletetrCustomer(this)"  id="htfrmmanagerhotelimgdelete_'+trIdCustomer
           +'"></td></tr>' ;       
       }
  }
  if(!flg_exist){
     strtbl = htfrmmanagerhotel_createDefautCus(current_id);
  }
  $('#htfrmmanagerhotel_divCustomer').find('table:eq(0)').html(''); 
  $('#htfrmmanagerhotel_roomCustomerId').val(id); 
  $('#htfrmmanagerhotel_divCustomer').find('table:eq(0)').append(strtblheader); 
  $('#htfrmmanagerhotel_divCustomer').find('table:eq(0)').append(strtbl); 
  title ='Thông tin khách hàng';
  $('#htfrmmanagerhotel_divCustomer').dialog('option', 'title',title);  
  $('#htfrmmanagerhotel_divCustomer').dialog('open'); 
   $('.jscontextBtn').hide();     
}

function htfrmmanagerhotel_createDefautCus(current_id){
    index = ht_obj.indexNewProduct;
    trIdCustomer = current_id+ '_'+ index +'_new';
     ht_obj.indexNewProduct ++;
    strtbl ='<tr id="htfrmmanagerhoteltr_'+trIdCustomer+'"><td id="htfrmmanagerhoteltdname_'+trIdCustomer+'"><input maxlength="35" style="width:98%" id="htfrmmanagerhoteltxtname_'+trIdCustomer+'" type="text"/></td><td id="htfrmmanagerhoteltdcmnd_'+trIdCustomer+'">'+'<input style="width:98%"  maxlength="10" id="htfrmmanagerhoteltxtcmnd_'+trIdCustomer+'" type="text"/>'+
           '</td><td id="htfrmmanagerhoteltdage_'+trIdCustomer+'">'+'<input style="width:98%" maxlength="2"  id="htfrmmanagerhoteltxtage_'+trIdCustomer+'" type="text"/>'+'</td><td id="htfrmmanagerhoteltdaddress_'+trIdCustomer+'">'+ '<input style="width:98%" maxlength="60" id="htfrmmanagerhoteltxtaddress_'+trIdCustomer+'" type="text"/>'
           +'</td><td id="htfrmmanagerhoteltdsex_'+trIdCustomer+'">'+'<select  style="width:98%" id="htfrmmanagerhoteltxtsex_'+trIdCustomer+'"><option value="0">Nam</option><option value="1">Nữ</option></select>'
           +'</td><td id="htfrmmanagerhoteltdmoto_'+trIdCustomer+'">'+'<input style="width:98%" id="htfrmmanagerhoteltxtmoto_'+trIdCustomer+'" type="text"/>'+'</td><td id="htfrmmanagerhoteltdedit_'+trIdCustomer+'"><img heigth="16" onclick="htfrmmanagerhotel_saveCustomer(this)" class="imgSmall" src="adagps.com/images/menu/save.png" id="htfrmmanagerhotelimgedit_'+trIdCustomer
           +'">&nbsp;&nbsp;<img class="imgSmall"  src="adagps.com/images/menu/red_delete.png" onclick="htfrmmanagerhotel_deletetrCustomer(this)" id="htfrmmanagerhotelimgdelete_'+trIdCustomer
           +'"></td></tr>' ;    
    return strtbl;
}

function htfrmmanagerhotel_saveCustomer(obj){
    //alert($(obj).attr('id'));
    arrName = $(obj).attr('id').split('_');
    
    generate_id = arrName[1];
    dum_id = arrName[2];
    flagNew = 1;
    cusnew ='';
    if(arrName.length==4){
      flagNew= 0;  
      cusnew ='_new';
    }
    postfix = generate_id+'_'+dum_id+cusnew;
    name = $.trim($('#htfrmmanagerhoteltxtname_'+postfix).val());
    cmnd = $.trim($('#htfrmmanagerhoteltxtcmnd_'+postfix).val());
    age = $.trim($('#htfrmmanagerhoteltxtage_'+postfix).val());
    address = $.trim($('#htfrmmanagerhoteltxtaddress_'+postfix).val());
    sex = $.trim($('#htfrmmanagerhoteltxtsex_'+postfix).val());
    moto = $.trim($('#htfrmmanagerhoteltxtmoto_'+postfix).val());
    xajax_htfrmmanagerhotel_saveCustomer(generate_id,name,cmnd,age,address,sex,moto,dum_id,flagNew);
    
    postfixtd = generate_id+'_'+dum_id;
    htfrmmanagerhotel_assignTrCus(postfix,postfixtd,name,cmnd,age,address,sex,moto);
    ht_obj.arrCustomer[$('#htfrmmanagerhotel_roomCustomerId').val()][dum_id]=ht_obj.addCustomer(name,cmnd,age,address,sex,moto);
}

function htfrmmanagerhotel_assignTrCus(postfix,postfixtd,name,cmnd,age,address,sex,moto){
   
    $obj = $('#htfrmmanagerhoteltr_'+postfix);
    $obj.text('');
    $obj.attr('id','htfrmmanagerhoteltr_'+postfixtd);
    tdname = '<td id="htfrmmanagerhoteltdname_'+postfixtd+'">'+name+'</td>';
    $obj.append(tdname);
     tdname = '<td id="htfrmmanagerhoteltdcmnd_'+postfixtd+'">'+cmnd+'</td>';
    $obj.append(tdname);
     tdname = '<td id="htfrmmanagerhoteltdage_'+postfixtd+'">'+age+'</td>';
    $obj.append(tdname);
     tdname = '<td id="htfrmmanagerhoteltdaddress_'+postfixtd+'">'+address+'</td>';
    $obj.append(tdname);
    namesex = (sex==0?'Nam':'Nữ');
     tdname = '<td id="htfrmmanagerhoteltdsex_'+postfixtd+'">'+namesex+'</td>';
    $obj.append(tdname);
     tdname = '<td id="htfrmmanagerhoteltdmoto_'+postfixtd+'">'+moto+'</td>';
    $obj.append(tdname);
    tdname = '<td id="htfrmmanagerhoteltdmoto_'+postfixtd+'"><img heigth="16" onclick="htfrmmanagerhotel_editCustomer(this)" class="imgSmall" src="adagps.com/images/menu/edit.png" id="htfrmmanagerhotelimgedit_'+postfixtd
           +'">&nbsp;&nbsp;<img class="imgSmall"  src="adagps.com/images/menu/red_delete.png" onclick="htfrmmanagerhotel_deletetrCustomer(this)" id="htfrmmanagerhotelimgdelete_'+postfixtd
           +'"></td>';
    $obj.append(tdname);
    
}

function htfrmmanagerhotel_addtrCustomer(){
    current_id = ht_obj.arrRoom[$('#htfrmmanagerhotel_roomCustomerId').val()].current_id;
    strtbl = htfrmmanagerhotel_createDefautCus(current_id);
    $('#htfrmmanagerhotel_divCustomer').find('table:eq(0)').append(strtbl); 
}

function htfrmmanagerhotel_deletetrCustomer(obj){
    if(!checkNew(obj)){
        generate_id = getId($(obj).attr('id'),1);
        dum_id = getId($(obj).attr('id'),2);
        ht_obj.arrCustomer[$('#htfrmmanagerhotel_roomCustomerId').val()][dum_id]=ht_obj.addCustomer('','','','','','');
        xajax_htfrmmanagerhotel_deleteCustomer(generate_id,dum_id);
    }
    
    $(obj).parents('tr:eq(0)').remove(); 
}

function checkNew(obj){
   arr = $(obj).attr('id').split('_');
    if(arr[arr.length-1]=='new'){
       return true; 
    }
    return false;
}

function htfrmmanagerhotel_editCustomer(obj){
   var objtr = $(obj).parents('tr:eq(0)');
    arrName = $(obj).attr('id').split('_');
    dum_id = arrName[2];
    postfix = arrName[1]+'_'+dum_id; 
    name = $('#htfrmmanagerhoteltdname_'+postfix).text();
    cmnd = $('#htfrmmanagerhoteltdcmnd_'+postfix).text();
    age = $('#htfrmmanagerhoteltdage_'+postfix).text();
    address = $('#htfrmmanagerhoteltdaddress_'+postfix).text();
    sex = $('#htfrmmanagerhoteltdsex_'+postfix).text();
    moto = $('#htfrmmanagerhoteltdmoto_'+postfix).text();
    
    objtr.html('');
    tdname = '<td id="htfrmmanagerhoteltdname_'+postfix+'"><input id="htfrmmanagerhoteltxtname_'+postfix+'" class="width98" type="text" value="'+name+'" maxlength="35"/></td>';
    objtr.append(tdname);
    tdname = '<td id="htfrmmanagerhoteltdcmnd_'+postfix+'"><input id="htfrmmanagerhoteltxtcmnd_'+postfix+'" class="width98" type="text" value="'+cmnd+'" maxlength="35"/></td>';
    objtr.append(tdname);
    tdname = '<td id="htfrmmanagerhoteltdage_'+postfix+'"><input id="htfrmmanagerhoteltxtage_'+postfix+'" class="width98" type="text" value="'+age+'" maxlength="35"/></td>';
    objtr.append(tdname);
    tdname = '<td id="htfrmmanagerhoteltdaddress_'+postfix+'"><input id="htfrmmanagerhoteltxtaddress_'+postfix+'" class="width98" type="text" value="'+address+'" maxlength="35"/></td>';
    objtr.append(tdname);
    selected = (sex=='Nam'?'':'selected=selected');    
    tdname = '<td id="htfrmmanagerhoteltdsex_'+postfix+'">'+'<select  style="width:98%" id="htfrmmanagerhoteltxtsex_'+postfix+'"><option value="0">Nam</option><option value="1" '+selected+'>Nữ</option></select>'+'</td>';
    objtr.append(tdname);
    tdname = '<td id="htfrmmanagerhoteltdmoto_'+postfix+'"><input id="htfrmmanagerhoteltxtmoto_'+postfix+'" class="width98" type="text" value="'+moto+'" maxlength="35"/></td>';
    objtr.append(tdname);
    tdname = '<td id="htfrmmanagerhoteltdmoto_'+postfix+'"><img heigth="16" onclick="htfrmmanagerhotel_saveCustomer(this)" class="imgSmall" src="adagps.com/images/menu/save.png" id="htfrmmanagerhotelimgsave_'+postfix
           +'">&nbsp;&nbsp;<img class="imgSmall"  src="adagps.com/images/menu/undo.png" onclick="htfrmmanagerhotel_undoTrCustomer(this)" id="htfrmmanagerhotelimgundo_'+postfix
           +'"></td>';
    objtr.append(tdname);
    
    
}

function htfrmmanagerhotel_undoTrCustomer(obj){
    arrName = $(obj).attr('id').split('_');
    flagnew = false;
    dum_id = arrName[2];
    postfix = arrName[1]+'_'+dum_id;

    if(arrName.length==4){
       $(obj).parents('tr:eq(0)').remove();  
    }else{
      temp =  ht_obj.arrCustomer[$('#htfrmmanagerhotel_roomCustomerId').val()][dum_id];
      name = temp.name;
      cmnd = temp.cmnd;
      age = temp.age;
      address = temp.address;
      sex = temp.sex;
      moto = temp.moto;
      htfrmmanagerhotel_assignTrCus(postfix,postfix,name,cmnd,age,address,sex,moto);
    }    
     
}
assignTime();
executeAction(); 
xajax_htfrmmanagerhotel_loadGrid();
xajax_htfrmmanagerhotel_loadMenu();
xajax_htfrmmanagerhotel_Init();
xajax_htfrmmanagerhotel_loadProduct(); 
xajax_SetConfigure(); 
function htfrmmanagerhotel_totalAll(){
    $totalProduct =0;
    $('#htfrmmanagerhotel_billOrder').find('.PriceProduct').each(function(){
       price = $(this).text() ;
       price=price.replace(/,/gi,'');
       $totalProduct +=  parseInt(price);       
    });
    $('#htfrmmanagerhotel_tdtotalProduct').text(formatMoney($totalProduct));
    
    roomPrice = $.trim($('#htfrmmanagerhotel_tdroomPrice').text());
    roomPrice=roomPrice.replace(/,/gi,'');
    $totalAll = $totalProduct + parseInt(roomPrice);
    
    $('#htfrmmanagerhotel_tdAllTotal').text(formatMoney($totalAll)); 
    
}
function formatMoney(money){
   // result ='';
   money = parseInt(money);
    money +='';
    length = money.length;
    index = Math.floor((length-1)/3);
    result='';
    for(i=0;i<index;i++){
       result =  ','+money.substr(money.length-3,3)+result;
       money= money.substr(0,money.length-3);
    }
    return money+result;
}
